# Define the project name.
PROJECT(libABF)
SET(ABF_VERSION "0.1.2")

CMAKE_MINIMUM_REQUIRED(VERSION 2.4.3)
SET(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake/Modules)


# -- for CMake 2.6.x
IF(COMMAND cmake_policy)
# -- pass fully qualified libraries directly to linker
    CMAKE_POLICY(SET CMP0003 NEW)
# -- use automatic escaping in add_definitions
    CMAKE_POLICY(SET CMP0005 NEW)
    SET(ESCAPE "")
ELSE(COMMAND cmake_policy)

# -- pre 2.6 needs escaping in add_definitions
	IF(CMAKE_GENERATOR MATCHES ".*Makefiles")
		SET(ESCAPE "\\\"")
	ELSE(CMAKE_GENERATOR MATCHES ".*Makefiles")
		SET(ESCAPE "\\\\\"")
	ENDIF(CMAKE_GENERATOR MATCHES ".*Makefiles")

ENDIF(COMMAND cmake_policy)

ADD_DEFINITIONS(-DABF_VERSION="${ESCAPE}${ABF_VERSION}${ESCAPE}" -DHAVE_CONFIG_H=1)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -g -O0" CACHE INTERNAL "")

IF(CMAKE_COMPILER_IS_GNUC)
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing -Wno-deprecated" CACHE INTERNAL "")
ENDIF(CMAKE_COMPILER_IS_GNUC)

IF(APPLE)
   SET(CMAKE_SHARED_LINKER_FLAGS "-single_module" CACHE INTERNAL "")
ENDIF(APPLE)

##########################
# Endianness
##########################

INCLUDE(TestBigEndian)
TEST_BIG_ENDIAN(WORDS_BIGENDIAN)
IF(WORDS_BIGENDIAN)
       ADD_DEFINITIONS("-D__BIG_ENDIAN__")
ENDIF(WORDS_BIGENDIAN)

INCLUDE(config/UsePkgConfig.cmake)



FIND_PACKAGE(ZLIB)

#######################
# LibXML2
#######################

PKGCONFIG_FOUND(libxml-2.0 LIBXML2_FOUND)
IF(LIBXML2_FOUND)
	PKGCONFIG(libxml-2.0 LIBXML2_INCLUDE_PATH LIBXML2_LIBADD LIBXML2_LIBRARIES LIBXML2_CFLAGS)
	IF (NOT LIBXML2_INCLUDE_PATH MATCHES ".*/libxml2$")
		SET(LIBXML2_INCLUDE_PATH "${LIBXML2_INCLUDE_PATH}/libxml2")
	ENDIF (NOT LIBXML2_INCLUDE_PATH MATCHES ".*/libxml2$")
	SET(HAVE_LIBXML 1)
ELSE(LIBXML2_FOUND)
	INCLUDE(config/FindLibxml2.cmake)
ENDIF(LIBXML2_FOUND)

IF(LIBXML2_FOUND)
	MESSAGE(STATUS
		"LibXml2 has been found:")
	MESSAGE(STATUS
		"\tCFLAGS : ${LIBXML2_INCLUDE_PATH}")
	MESSAGE(STATUS
		"\tLDFLAGS: ${LIBXML2_LIBRARIES}")
	ADD_DEFINITIONS(-DABF_HAVE_LIBXML)
ELSE(LIBXML2_FOUND)
	MESSAGE(STATUS "LibXml2 not found.")
ENDIF(LIBXML2_FOUND)

######################
# Build the config.h file !
######################

INCLUDE(CheckIncludeFiles)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckLibraryExists)

CHECK_INCLUDE_FILES(memory.h HAVE_MEMORY_H)
CHECK_INCLUDE_FILES(stdint.h HAVE_STDINT_H)
CHECK_INCLUDE_FILES(stdlib.h HAVE_STDLIB_H)
CHECK_INCLUDE_FILES(strings.h HAVE_STRINGS_H)
CHECK_INCLUDE_FILES(sys/stat.h HAVE_SYS_STAT_H)
CHECK_INCLUDE_FILES(sys/types.h HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h" STDC_HEADERS)

# Now generate the configure file
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/lib/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/lib/config.h)


# Add the root of the binary distribution to the include directories !
# Do it in order to fetch config.h, however, we may generate config.h in the
# src directory.
SET(LIBABF_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/src/)

ADD_SUBDIRECTORY(lib/)
ADD_SUBDIRECTORY(libcpp/)
ADD_SUBDIRECTORY(test/)
ADD_SUBDIRECTORY(tool/)


ADD_CUSTOM_TARGET (deb checkinstall -D --pkgname="libabf"
                     --pkgversion="${ABF_VERSION}"
                     --install=0
                     --deldoc=1
                     --backup=0
                     --pkglicense="LGPL"
                     --maintainer="klaus.blindert@web.de"
                     --pkggroup=""
                     --requires="libxml2-dev")

SET (CPACK_PACKAGE_DESCRIPTION_SUMMARY "")
SET (CPACK_PACKAGE_VENDOR "klaus.blindert@web.de")
# SET (CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README")
SET (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")

SET (CPACK_GENERATOR TGZ)
SET (CPACK_SOURCE_PACKAGE_FILE_NAME
  "libabf-${ABF_VERSION}")
SET (CPACK_SOURCE_GENERATOR TGZ)
SET (CPACK_SOURCE_IGNORE_FILES
   "CMakeCache.txt"
   "${CPACK_SOURCE_PACKAGE_FILE_NAME}"
   ".*.tar.gz"
   "CPack.*"
   ".*deb"
   "CMakeFiles"
   "CMakeCache.txt"
   "install_manifest.txt"
   "Makefile"
   "_CPack_Packages"
   "po"
   ".*~"
   "cmake_install.cmake"
   "progress.make"
   ".svn"
   "doc")
INCLUDE (CPack)

ADD_CUSTOM_TARGET (really-clean rm -fr
                     cmake_install.cmake
                     */cmake_install.cmake
                     *.tar.gz
                     *.deb
                     CPackConfig.cmake
                     cmake_uninstall.cmake
                     CMakeFiles
                     CPackSourceConfig.cmake
                     install_manifest.txt
                     Makefile
                     */Makefile
                     _CPack_Packages
                     *~ */*~ */*/*~) 

